import 'package:card_swiper/card_swiper.dart';
import 'package:distribution_mall_flutter/entity/home.dart';
import 'package:distribution_mall_flutter/provider/home/home.dart';
import 'package:distribution_mall_flutter/provider/order/order.dart';
import 'package:distribution_mall_flutter/utils/image_loader.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:skeletonizer/skeletonizer.dart';

class BannerInfoWidget extends ConsumerStatefulWidget {
  const BannerInfoWidget({super.key});

  @override
  ConsumerState createState() => _BannerInfoWidget();
}

class _BannerInfoWidget extends ConsumerState<BannerInfoWidget> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((msg) {
      ref.read(homeProvider.notifier).getBanner();
      // Future.delayed(Duration(seconds: 2), () {
      //   ref.watch(userInfoProvider.notifier).getUserInfo();
      // });
    });
  }

  @override
  Widget build(BuildContext context) {
    List<BannerInfo> bannerList =
        ref.watch(homeProvider.select((value) => value.bannerList));
    bool bannerLoading =
        ref.watch(homeProvider.select((value) => value.bannerLoading));
    bool hasOrderTrace =
        ref.watch(orderProvider.select((value) => value.hasOrderTrace));
    return Visibility(
        visible: bannerLoading || bannerList.isNotEmpty,
        child: Padding(
          padding: const EdgeInsets.fromLTRB(14.6, 14.6, 14.6, 0),
          child: Skeleton.replace(
              width: double.infinity,
              height: hasOrderTrace ? 65.5 : 131,
              child: ClipRRect(
                borderRadius: const BorderRadius.all(Radius.circular(16)),
                child: Swiper(
                  autoplay: bannerList.length > 1 ? true : false,
                  duration: 1000,
                  loop: bannerList.length > 1,
                  // autoplayDelay: 5000,
                  itemBuilder: (BuildContext context, int index) {
                    return ImageLoader.imageNet(
                      bannerList[index].bannerPicUrl ?? '',
                      fit: BoxFit.cover,
                    );
                  },
                  itemCount: bannerList.length,
                ),
              )),
        ));
  }
}
